package org.mbds.wolf.seql;

import java.util.Arrays;

/**
 * 
 * @author amlesas
 *
 */
public class APDUResponse {

	private byte[] statusWord;
	private byte[] data;
	private byte[] response;
	
	/**
	 * Default constructor
	 */
	public APDUResponse() {
		
	}

	/**
	 * APDUResponse constructor
	 * @param apdu
	 * @param encoding
	 */
	public APDUResponse(byte[] apdu) {
		setResponse(apdu);
	}

	/**
	 * @return the statusWord
	 */
	public byte[] getStatusWord() {
		return statusWord;
	}

	/**
	 * @param statusWord the statusWord to set
	 */
	public void setStatusWord(byte[] statusWord) {
		this.statusWord = statusWord;
		if (data==null) {
			response = statusWord.clone();
		} else {
			setData(data);
		}
	}

	/**
	 * @return the data
	 */
	public byte[] getData() {
		return data;
	}

	/**
	 * @param data the data to set
	 */
	public void setData(byte[] data) {
		if (response==null)
			this.response = new byte[data.length+2];
		System.arraycopy(data, 0, response, 0, data.length);
		if (statusWord!=null)
			System.arraycopy(statusWord, 0, response, data.length, statusWord.length);
		this.data = data;
	}

	/**
	 * @return the response
	 */
	public byte[] getResponse() {
		return response;
	}

	/**
	 * @param response the response to set
	 */
	public void setResponse(byte[] response) {
		if (response.length>2)
			setData(Arrays.copyOfRange(response, 0, response.length-2));
		if (response.length>1)
			setStatusWord(Arrays.copyOfRange(response, response.length-2, response.length));
	}

}
